package cn.kgc.listener.rabbitmq;

import cn.kgc.listener.rabbitmq.chat.ChatMessageListener;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author YC
 */
@Configuration
public class ChatRabbitmqConfig {

    @Bean
    public Queue chatQueue() {
        return new Queue("chatQueueName", true, false, false);
    }

    @Bean
    public DirectExchange chatExchange() {
        return new DirectExchange("chatExchange", true, false);
    }

    @Bean
    public Binding chatBinding() {
        return BindingBuilder.bind(chatQueue()).to(chatExchange()).with("chat");
    }

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer3(
            ConnectionFactory connectionFactory, ChatMessageListener chatMessageListener
    ) {
        // 创建消息监听器
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        // 设置连接工厂
        container.setConnectionFactory(connectionFactory);
        // 设置监听器
        container.setMessageListener(chatMessageListener);
        // 添加需要监听的队列
        container.setQueues(chatQueue());
        // 监听器手动确认
        container.setExposeListenerChannel(true);
        // 监听器确认模式
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        // 设置并发监听器数量
        container.setConcurrentConsumers(1);
        return container;
    }

}
